package com.seednia.module.core.resource.ui.web;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.seednia.foundation.framework.service.AbstractWebRequestController;
import com.seednia.foundation.framework.service.RequestContext;
import com.seednia.foundation.framework.service.ServiceContainer;
import com.seednia.foundation.framework.service.ServiceException;
import com.seednia.foundation.framework.util.RecordUtils;
import com.seednia.module.core.config.dto.Constants;
import com.seednia.module.core.config.dto.SettingDto;
import com.seednia.module.core.config.service.IConfigService;
import com.seednia.module.core.resource.dto.ResourceCriteria;
import com.seednia.module.core.resource.dto.ResourceDto;
import com.seednia.module.core.resource.service.IResourceService;

@Controller
public class ResourceController extends AbstractWebRequestController {
	
	private IResourceService service = null;
	
	private IConfigService configService = null;
	
	public ResourceController() 
	{
	}
	
	public IConfigService getConfigService() {
		if (this.configService == null) {
			configService = ServiceContainer.getInstance().getByType(IConfigService.class);
		}
		return configService;
	}
	public void setConfigService(IConfigService configService) {
		this.configService = configService;
	}


	@RequestMapping(value="/file/{sid}/{filename}", method = RequestMethod.GET)
	public ResponseEntity<byte[]> getFile( @PathVariable(value="sid") long sid
			, @PathVariable(value="filename") String filename // dummy
			, HttpServletRequest request) 
			throws IOException 
	{
		RequestContext reqCtx = super.createRequestContext(request);
		ResourceDto resourceDto = null;
		if ( RecordUtils.isSidPositive(sid)) {
			ResourceCriteria criteria = new ResourceCriteria(sid);
			try {
				resourceDto = this.getService().getResource(reqCtx, criteria);
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			resourceDto = new ResourceDto();
			SettingDto dirRoot = getConfigService().getSystemSetting(Constants.SETTING_SITE_FILE_DIR);
			resourceDto.setRepositoryUri(dirRoot.getValue());
			resourceDto.setFileName(filename);
			resourceDto.setContentType(URLConnection.guessContentTypeFromName(filename));
		}

		File file = this.getService().fileFromRecord(resourceDto);
		
		InputStream fileIs = new BufferedInputStream(new FileInputStream(file));
		//URLConnection.guessContentTypeFromStream(fileIs);
		
		byte[] bytes = null;
		bytes = IOUtils.toByteArray(fileIs);
		
		MediaType mediaType = MediaType.valueOf(resourceDto.getContentType());
		
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(mediaType);
		headers.setContentLength(bytes.length);
		//headers.setContentDispositionFormData(name, filename)( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );
		headers.set("Content-Disposition", "attachment;filename="+resourceDto.getFileName());
		
		return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.CREATED);
	}

	public IResourceService getService() {
		if (service == null) {
			service = ServiceContainer.getInstance().getByType(IResourceService.class);
		}
		return service;
	}

	public void setservice(IResourceService service) {
		this.service = service;
	}

	///
	
}
